package cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.controller;

import cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.entity.Equipment;
import cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.pojo.request.EquipmentRequest;
import cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.service.EquipmentService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备台账控制器
 *
 * @author PANL
 * @since 2023/12/04 09:18
 */
@RestController
@ApiResource(name = "设备台账")
public class EquipmentController {

    @Resource
    private EquipmentService equipmentService;

    /**
     * 添加设备台账
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    @PostResource(name = "添加设备台账", path = "/equipment/add")
    public ResponseData<Equipment> add(@RequestBody @Validated(EquipmentRequest.add.class) EquipmentRequest equipmentRequest) {
        Equipment one = equipmentService.getOne(new LambdaQueryWrapper<Equipment>().eq(Equipment::getEqpCode, equipmentRequest.getEqpCode()));
        if (one != null) {
            return new ErrorResponseData<Equipment>("500", "设备id重复");
        }
        equipmentService.add(equipmentRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除设备台账
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    @PostResource(name = "删除设备台账", path = "/equipment/delete")
    public ResponseData<?> delete(@RequestBody @Validated(EquipmentRequest.delete.class) EquipmentRequest equipmentRequest) {
        equipmentService.del(equipmentRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 批量删除设备台账
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    @PostResource(name = "批量删除设备台账", path = "/equipment/batchDelete")
    public ResponseData<?> batchDelete(@RequestBody @Validated(BaseRequest.batchDelete.class) EquipmentRequest equipmentRequest) {
        equipmentService.batchDelete(equipmentRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑设备台账
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    @PostResource(name = "编辑设备台账", path = "/equipment/edit")
    public ResponseData<?> edit(@RequestBody @Validated(EquipmentRequest.edit.class) EquipmentRequest equipmentRequest) {
        equipmentService.edit(equipmentRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 查看设备台账详情
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    @GetResource(name = "查看设备台账详情", path = "/equipment/detail")
    public ResponseData<Equipment> detail(@Validated(EquipmentRequest.detail.class) EquipmentRequest equipmentRequest) {
        return new SuccessResponseData<>(equipmentService.detail(equipmentRequest));
    }

    /**
     * 获取设备台账列表
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    @GetResource(name = "获取设备台账列表", path = "/equipment/list")
    public ResponseData<List<Equipment>> list(EquipmentRequest equipmentRequest) {
        return new SuccessResponseData<>(equipmentService.findList(equipmentRequest));
    }

    /**
     * 获取设备台账列表（带分页）
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    @GetResource(name = "获取设备台账列表（带分页）", path = "/equipment/page")
    public ResponseData<PageResult<Equipment>> page(EquipmentRequest equipmentRequest) {
        return new SuccessResponseData<>(equipmentService.findPage(equipmentRequest));
    }

}
